package com.keyinong.parkin.home.ticket;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.parkin.R;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.keyinong.parkin.home.adapter.TicketAdapter;
import com.keyinong.parkin.home.bean.ImageFileName;
import com.keyinong.parkin.home.bean.TicketBean;
import com.keyinong.parkin.internetmodel.ToolModel;
import com.keyinong.parkin.util.MyToast;
import com.keyinong.parkin.util.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class TicketInfo {
	private Context context;
	private View view;
	private PullToRefreshListView list_ticket;
	private TextView tv_noContent;
	private int ticketStatus;
	private int page = 1;
	private int limit = 10;
	private int index = 0;
	private String promptContent;
	Gson gson = new Gson();

	private ArrayList<TicketBean> ticketlist = new ArrayList<TicketBean>();

	public TicketInfo(Context context, View view, String promptContent,
			int ticketStatus) {
		super();
		this.context = context;
		this.view = view;
		this.promptContent = promptContent;
		this.ticketStatus = ticketStatus;
	}

	public void initRes() {
		list_ticket = (PullToRefreshListView) view
				.findViewById(R.id.list_ticket);
		tv_noContent = (TextView) view.findViewById(R.id.tv_noContent);
		tv_noContent.setText(promptContent);
		list_ticket.setMode(Mode.BOTH);
		list_ticket.setOnRefreshListener(onrefresh);
		getticket();
		list_ticket.setOnItemClickListener(itemclick);
	}

	private OnItemClickListener itemclick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent = new Intent(context, TicketDetailActivity.class);
			intent.putExtra("ticketbean", ticketlist.get(position - 1));
			context.startActivity(intent);
		}
	};

	private OnRefreshListener2<ListView> onrefresh = new OnRefreshListener2<ListView>() {

		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			page = 1;
			index = 0;
			ticketlist.clear();
			getticket();
		}

		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
			page++;
			index = page;
			getticket();
		}
	};

	private void getticket() {

		SharedPreferences sp = context.getSharedPreferences("spuser",
				Context.MODE_PRIVATE);
		String token = sp.getString("token", "");
		String parkid = sp.getString("park_id", "");
		ToolModel toolModel = new ToolModel(context);
		toolModel.getticket(token, parkid, ticketStatus, page, limit,
				new JsonHttpResponseHandler() {
					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable e, JSONObject errorResponse) {
						try {
							if (NetWorkUtil.isNetworkToast(context)) {
								String message = errorResponse
										.getString("message");
								MyToast.mToast(context, message);
							}
							list_ticket.onRefreshComplete();
						} catch (JSONException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						super.onFailure(statusCode, headers, e, errorResponse);
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
//						Log.e("lh", response.toString());
						try {
							Gson gson = new Gson();
							int status_code = response.getInt("status_code");
							String message = response.getString("message");
							if (status_code == 200) {
								JSONObject data = response
										.getJSONObject("data");
								JSONObject ticket_list = data
										.getJSONObject("ticket_list");
								JSONArray datalist = ticket_list
										.getJSONArray("data");
								for (int i = 0; i < datalist.length(); i++) {
									// ArrayList<ImageFileName> imglist = new
									// ArrayList<ImageFileName>();
									TicketBean ticketbean = gson.fromJson(
											datalist.getString(i).toString(),
											TicketBean.class);
									ticketlist.add(ticketbean);
									// JSONObject object = datalist
									// .getJSONObject(i);
									// String id = object.getString("id");
									// String user_id = object
									// .getString("user_id");
									// String subject = object
									// .getString("subject");
									// String content = object
									// .getString("content");
									// int status = object.getInt("status");
									// String created_at = object
									// .getString("created_at");
									// JSONArray imgarray = object
									// .getJSONArray("image_list");
									// for (int y = 0; y < imgarray.length();
									// y++) {
									// JSONObject img = imgarray
									// .getJSONObject(y);
									// String filename = img
									// .getString("filename");
									// imglist.add(new ImageFileName(filename));
									// }
									// ticketlist.add(new TicketBean(id,
									// user_id,
									// subject, content, status,
									// created_at,imglist));
									// ticketlist.add(ticketbean);
								}
								if (ticketlist.size() != 0) {
									tv_noContent.setVisibility(View.GONE);
								} else {
									tv_noContent.setVisibility(View.VISIBLE);
								}
								TicketAdapter adapter = new TicketAdapter(
										context, ticketlist, 1);
								list_ticket.setAdapter(adapter);
								list_ticket.getmRefreshableView().setSelection(
										(index - 1) * limit);
							} else {
								MyToast.mToast(context, message);
							}
							list_ticket.onRefreshComplete();
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}

}
